#include "widget.h"
#include "ui_widget.h"
#include<QFileDialog>
#include<QDebug>
#include<QMenuBar>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_Choose_clicked()
{
    QString path=QFileDialog::getOpenFileName(this,"打开文件","E:\\image\\","(*.bmp)");

        img.load(path);
        ui->imagelabel->setPixmap(QPixmap::fromImage(img));
}

void Widget::on_Gray_clicked()
{
    unsigned char*data=img.bits();
    int width=img.width();//获取图像宽度
        int height=img.height();//获取图像高度
        int bytePerLine=(width*24+31)/8;//图像每行字节对齐

        graydata=new unsigned char[bytePerLine*height];//存储处理后的数据

        unsigned char r,g,b;//定义rgb
        for (int i=0;i<height;i++)
        {
            for (int j=0;j<width;j++)
            {
                r = *(data+2);
                g = *(data+1);
                b = *data;
                //RGB转灰度的计算公式Gray=(R*30+G*59+B*11)/100
                graydata[i*bytePerLine+j*3]  =(r*30+g*59+b*11)/100;
                graydata[i*bytePerLine+j*3+1]=(r*30+g*59+b*11)/100;
                graydata[i*bytePerLine+j*3+2]=(r*30+g*59+b*11)/100;

                data+=4;
            }
        }
        //显示图像
        grayImg=new QImage(graydata,width,height,bytePerLine,QImage::Format_RGB888);
        ui->imagelabel->setPixmap(QPixmap::fromImage(*grayImg));
}

void Widget::on_Rotate_clicked()
{
    ui->horizontalSlider->setVisible(true);//显示滑动条
}

void Widget::on_shrink_clicked()
{
    QImage* imgScaled = new QImage;
        int width=ui->imagelabel->width()/2;
        int height=ui->imagelabel->height()/2;
        *imgScaled=img.scaled(width,height,Qt::KeepAspectRatio);//对半缩放
        ui->imagelabel->setPixmap(QPixmap::fromImage(*imgScaled));
}
void Widget::on_horizontalSlider_valueChanged(int value)
{
    qDebug()<<value;//输出旋转角度
        QImage* imgRatate = new QImage;
        QMatrix matrix;
        matrix.rotate(value);
        *imgRatate = img.transformed(matrix);
        ui->imagelabel->setPixmap(QPixmap::fromImage(*imgRatate));
}

void Widget::on_enlarge_clicked()
{
    QImage* imgScaled = new QImage;
        int width=ui->imagelabel->width()*2;
        int height=ui->imagelabel->height()*2;
        *imgScaled=img.scaled(width,height,Qt::KeepAspectRatio);//对半缩放
        ui->imagelabel->setPixmap(QPixmap::fromImage(*imgScaled));
}
